package com.lanou.beautifulwardrobe.space.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.beautifulwardrobe.R;

/**
 * Created by dllo on 15/10/26.
 */
public class SpaceRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private String[] firstTitle = {"我的收货地址", "我的优惠券", "我的钱包", "我的美丽币", "我的宝贝", "我的主题", "我的品牌", "我的专辑", "签到分享", "我的关注"};
    private String[] secondTitle = {"签到中心", "我的粉丝", "给美丽衣橱打5分", "设置"};
    private int[] firstImageId = {R.mipmap.kimagel_addr_list, R.mipmap.kimagel_coupon, R.mipmap.kimagel_wallet, R.mipmap.kimagel_ykcoin, R.mipmap.kimagel_x_product, R.mipmap.kimagel_x_activity, R.mipmap.kimagel_x_brand, R.mipmap.kimagel_x_album, R.mipmap.kimagel_x_share, R.mipmap.kimagel_x_follow};
    private int[] secondImageId = {R.mipmap.kimagel_signcenter, R.mipmap.kimagel_x_fans, R.mipmap.kimagel_dafen, R.mipmap.kimagel_setting};
    private int[] type = {0, 1, 2, 3, 5, 6, 7, 8, 9, 10};


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup view, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType) {
            case TYPE0:
                View firstView = LayoutInflater.from(view.getContext()).inflate(R.layout.space_state_item, null);
                holder = new FirstViewHolder(firstView);
                break;
            case TYPE1:
                View secondView = LayoutInflater.from(view.getContext()).inflate(R.layout.space_state_line_item, null);
                holder = new SecondViewHolder(secondView);
                break;
        }
        return holder;
    }

    int temp0 = 0;
    int temp1 = 0;

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case TYPE0:
                FirstViewHolder firstHolder = (FirstViewHolder) holder;
                if (temp0 < firstTitle.length ) {
                firstHolder.spaceImage.setImageResource(firstImageId[temp0]);
                firstHolder.spaceTitle.setText(firstTitle[temp0]);
                temp0++;
            }
            break;

            case TYPE1:
                SecondViewHolder secondHolder = (SecondViewHolder) holder;
                if (temp1 < secondTitle.length ) {
                secondHolder.spacelineImage.setImageResource(secondImageId[temp1]);
                secondHolder.spacelineTitle.setText(secondTitle[temp1]);
                temp1++;
            }
            break;

        }

    }

    public static final int TYPE0 = 0;
    public static final int TYPE1 = 1;

    @Override
    public int getItemCount() {
        return firstTitle.length + secondTitle.length;
    }

    @Override
    public int getItemViewType(int position) {
        for (int i = 0; i < type.length; i++) {
            if (position == type[i]) {
                return TYPE0;
            }
        }
        return TYPE1;
    }

    class FirstViewHolder extends RecyclerView.ViewHolder {
        private ImageView spaceImage;
        private TextView spaceTitle;

        public FirstViewHolder(View view) {
            super(view);
            spaceImage = (ImageView) view.findViewById(R.id.spaceTitleImage);
            spaceTitle = (TextView) view.findViewById(R.id.spaceTitle);
        }
    }

    class SecondViewHolder extends RecyclerView.ViewHolder {
        private ImageView spacelineImage;
        private TextView spacelineTitle;

        public SecondViewHolder(View view) {
            super(view);
            spacelineImage = (ImageView) view.findViewById(R.id.spacelineImage);
            spacelineTitle = (TextView) view.findViewById(R.id.spacelineTitle);
        }
    }
}
